﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using MyBlog.DataAccess;
using MyBlog.Model;
using MyBlog.Common;
using AutoMapper;
using MyBlog.BusinessInterface;
using MyBlog.DataEntity;
using MyBlog.DataAccessInterface;
namespace MyBlog.Business
{
    public class BusinessWordComment : IBusinessWordComment
    {
        IDALWordComment _dalComment;
        IUnitOfWork _unitWork;
        public BusinessWordComment(IDALWordComment dalComment, IUnitOfWork unitWork)
        {
            _dalComment = dalComment;
            _unitWork = unitWork;
        }
        public void Add(WordCommentModel model)
        {
            var dataEntity = Mapper.Map<Tbl_WordComment>(model);
            dataEntity.CommentTime = DateTime.Now;
            _dalComment.Add(dataEntity);
            _unitWork.Commit();
            model.ID = dataEntity.ID;
        }

        public void Update(WordCommentModel model)
        {
            var dataEntity = _dalComment.Get(model.ID);
            dataEntity.Content = model.Content;
            _unitWork.Commit();
        }
        public void AddRange(IEnumerable<WordCommentModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_WordComment>>(models);
            _dalComment.AddRange(dataEntitys);
            _unitWork.Commit();
        }

        public void DeleteRange(IEnumerable<WordCommentModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_WordComment>>(models);
            _dalComment.DeleteRange(dataEntitys);
            _unitWork.Commit();
        }

        public List<WordCommentModel> GetAll(bool isTracking = true)
        {
            var entities = _dalComment.GetAll(isTracking);
            return Mapper.Map<List<WordCommentModel>>(entities);
        }

        public void Delete(params object[] primaryKey)
        {
            _dalComment.Delete(primaryKey);
            _unitWork.Commit();
        }

        public WordCommentModel Get(params object[] primaryKey)
        {
            var dataEntity = _dalComment.Get(primaryKey);
            return Mapper.Map<WordCommentModel>(dataEntity);
        }

        public void Delete(WordCommentModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_WordComment>();
            if (model.ID > 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            _dalComment.Delete(where);
            _unitWork.Commit();
        }

        public WordCommentModel Get(WordCommentModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_WordComment>();
            if (model.ID != 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            var dataEntity = _dalComment.Get(where);
            return Mapper.Map<WordCommentModel>(dataEntity);
        }

        public List<WordCommentModel> GetAll(WordCommentModel model, bool isTracking = true)
        {
            var where = LinqExpressionBuilder.True<Tbl_WordComment>();
            var entities = _dalComment.GetAll(where, isTracking);
            return Mapper.Map<List<WordCommentModel>>(entities);
        }


        public List<WordCommentModel> GetAll(WordCommentModel model, int page, int size, out int total, bool isTracking = true)
        {
            throw new NotImplementedException();
        }

        public void AddAgree(int id, string account)
        {
            var dataEntity = _dalComment.Get(id);
            dataEntity.AgreeUsers = string.IsNullOrWhiteSpace(dataEntity.AgreeUsers) ? account : dataEntity.AgreeUsers + "&" + account;
            _unitWork.Commit();
        }
    }
}
